package zsc.ruanc.practicaltraining.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import zsc.ruanc.practicaltraining.exception.ApiException;
import zsc.ruanc.practicaltraining.mapper.ComplaintMapper;
import zsc.ruanc.practicaltraining.model.domain.TComplaint;
import zsc.ruanc.practicaltraining.model.result.JsonResult;
import zsc.ruanc.practicaltraining.model.vo.TimeVo;
import zsc.ruanc.practicaltraining.model.vo.handleVo;
import zsc.ruanc.practicaltraining.service.ComplaintService;
import zsc.ruanc.practicaltraining.service.RabbitMQService;
import zsc.ruanc.practicaltraining.utils.ResultUtils;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * 投诉控制器
 */
@RestController
@RequestMapping("/complaint")
@Validated
@Api(tags = "投诉模块")
public class ComplaintController {

    @Autowired
    private ComplaintService complaintService;

    @Autowired
    private RabbitMQService rabbitMQService;


    @PostMapping("/insert")
    @ApiOperation(value = "用户投诉", httpMethod = "POST")
    public JsonResult insert(@Validated @RequestBody TComplaint vo, HttpServletRequest httpServletRequest) {
        int userId = (int) httpServletRequest.getAttribute("id");
        return ResultUtils.success(complaintService.insert(vo, userId), "已收到你的投诉，管理员会尽快处理");
    }

    @GetMapping("/delete/{id}")
    @ApiOperation(value = "删除投诉信息", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "信息id", dataType = "Integer", paramType = "path")
    public JsonResult delete(@NotNull(message = "不能为空") @Min(message = "id应大于1", value = 1) @PathVariable(value = "id", required = false) Integer id,
                             HttpServletRequest httpServletRequest) {

        int userId = (int) httpServletRequest.getAttribute("id");
        return ResultUtils.success(complaintService.delete(id, userId), "删除成功");
    }

    @GetMapping("/select")
    @ApiOperation(value = "用户查看自己的投诉列表", httpMethod = "GET")
    public JsonResult select(HttpServletRequest httpServletRequest) {

        int userId = (int) httpServletRequest.getAttribute("id");
        return ResultUtils.success(complaintService.select(userId));
    }


    @PostMapping("/update")
    @ApiOperation(value = "用户修改投诉信息 如果管理员已经处理完毕则不能修改", httpMethod = "POST")
    public JsonResult update(@Validated @RequestBody TComplaint complaint,
                             HttpServletRequest httpServletRequest) {
        if (complaint.getId() == null) {
            throw new ApiException(-1);
        }
        int userId = (int) httpServletRequest.getAttribute("id");
        return ResultUtils.success(complaintService.update(complaint, userId), "修改成功");
    }


    // -----------管理员权限--------------------------------


    @PostMapping("/adminSelect")
    @ApiOperation(value = "查询投诉列表（管理员|分页）", httpMethod = "POST")
    public JsonResult adminSelect(@Validated @RequestBody TimeVo vo) {

        return ResultUtils.success(complaintService.adminSelect(vo));
    }


    @PostMapping("/handle")
    @ApiOperation(value = "管理员处理投诉", httpMethod = "POST")
    public JsonResult handle(@Validated @RequestBody handleVo vo) {
        complaintService.handle(vo);
        int fromId = complaintService.getFromId(vo.getId());


        rabbitMQService.adminMailbox(fromId, "你的投诉(编号:" + String.valueOf(vo.getId()) + ")管理员已经处理，快去看一下吧");
        return ResultUtils.success();
    }




}
